﻿
using Dapper;
using HBase;

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Transactions;

namespace hCommand {
    /**
   *┌────────────────────────────────────────────────┐
   *│　描    述：Component                                                    
   *│　作    者：sgj                                              
   *│　版    本：1.0                                              
   *│　创建时间：2020/9/2 11:28:16                        
   *└────────────────────────────────────────────────┘
   **/
    public abstract class StockAddEngine<CS> : AbsLocEngine where CS : AbsCoreStock {
        public String boxCode;
        public int locId;

        protected override TransRes Execute(IDbConnection conn, IDbTransaction tran) {

            CoreWhLoc loc = conn.GetById<CoreWhLoc>(locId, tran);
            if (loc.maxStock - loc.countStock - loc.workingCount() == 0) {
                return new TransRes(locId + "已经满!");
            }

            before(conn, tran);
            checkStock(conn, tran);
            CS cs = createCS(conn,  tran);
            cs.boxCode= boxCode;    
            cs.locId = locId;
            cs.status = (int)StockStatus.库存;
            if (cs.id == 0) {
              
                conn.Insert(cs, tran);
            } else {
                conn.Update(cs, tran);
            }

            UpdataWhLoc(conn, tran, locId, 0);
            return afterIntask(conn, tran, cs, loc);
        }

        protected virtual TransRes afterIntask(IDbConnection conn, IDbTransaction trans, CS cs, CoreWhLoc loc) {
            return new TransRes();
        }
        protected virtual void before(IDbConnection connection, IDbTransaction transaction) {
        }
        public abstract CS createCS(IDbConnection conn, IDbTransaction tran);



        public virtual void checkStock(IDbConnection Connection, IDbTransaction transaction) {
            string sql = @"select * from Core_stock where  status >={0} and status<={1}   and box_Code=@boxCode  ";
            sql = String.Format(sql, (int)StockStatus.库外, (int)StockStatus.库存);
            List<CS> stockList = Connection.Query<CS>(sql, new {
                boxCode
            }, transaction).ToList();
            if (stockList.Count != 0) {
                String info = boxCode + "重复的库位异常stock!";
                throw new Exception(info);
            }

        }
    }
}
